package com.yzdmw.toutiao.history;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.yzdmw.toutiao.databinding.OneHistoryItemBinding;
import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.detailsPage.DetailsActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class ItemHistoryFragment extends Fragment {

    private ItemHistoryViewModel mViewModel;
    private String message;
    private OneHistoryItemBinding binding;
    private MyListAdapter myListAdapter;
    public static ItemHistoryFragment newInstance(String message) {
        return new ItemHistoryFragment(message);
    }
    public ItemHistoryFragment(String message){
        this.message=message;
    }
    public ItemHistoryFragment(){}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.item_history_fragment, null, false);
        ListView list = view.findViewById(R.id.history_list);

        String[] listDataStr={"总书记参观的党史展览馆这些亮点不要错过","ewr","er","dgasfd"};
//        System.out.println(listDataStr);
        ArrayList<ItemData> listData=new ArrayList<>();
        for (String i :
                listDataStr) {
            listData.add(new ItemData(i));
        }
        if (message.equals("推荐")){
            listData.add(new ItemData("heiheihei"));
        }
        myListAdapter = new MyListAdapter(inflater, listData);
        list.setAdapter(myListAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intend = new Intent(requireActivity(), DetailsActivity.class);
                startActivity(intend);
            }
        });
        registerForContextMenu(list);
        return view;
    }

    @Override
    public void onCreateContextMenu(@NonNull @NotNull ContextMenu menu, @NonNull @NotNull View v, @Nullable @org.jetbrains.annotations.Nullable ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
//        menu.setHeaderTitle("操作：");
        menu.add(1,1,1,"删除记录");
    }

    @Override
    public boolean onContextItemSelected(@NonNull @NotNull MenuItem item) {
        //获取上下文菜单适配器
        AdapterView.AdapterContextMenuInfo cmi=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        //获取被选择的菜单位置
        int posMenu=cmi.position;
        //将菜单项与列表视图的条目相关联
//        Nbp_choosen_data =(Nbp_data)myListAdapter.getItem(posMenu);

        switch(item.getItemId()){
            case 1://删除选中项
//                System.out.println(""+myListAdapter+":"+posMenu);
                myListAdapter.listData.remove(posMenu);
                myListAdapter.notifyDataSetChanged();

                break;
            case 2://修改选中项
                //TODO
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(ItemHistoryViewModel.class);
        binding = OneHistoryItemBinding.inflate(getLayoutInflater());
        TextView content = binding.historyTitle;
        mViewModel.getContent().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String str) {
                content.setText(str);
            }
        });
        // TODO: Use the ViewModel
    }

    class MyListAdapter extends BaseAdapter {
        private LayoutInflater layoutInflater;
        private ArrayList<ItemData> listData;
        public MyListAdapter(LayoutInflater layoutInflater,ArrayList<ItemData> listData) {
            this.layoutInflater=layoutInflater;
            this.listData=listData;
        }
        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null) {
                viewHolder=new ViewHolder();
                view = layoutInflater.inflate(R.layout.one_history_item, null);
                viewHolder.history_title=(TextView)view.findViewById(R.id.history_title);
                viewHolder.history_content=(TextView)view.findViewById(R.id.history_content);
                viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);

                viewHolder.history_title.setText(listData.get(i).getContent());
                view.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder)view.getTag();
                viewHolder.history_title.setText(listData.get(i).getContent());
            }
//            viewHolder.imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            return view;
        }
    }
    static class ViewHolder {
        public TextView history_title;
        public TextView history_content;
        public ImageView imageView;
    }

    private void navigateToDetail(String articleID) {
        NavDirections navDirections = HistoryFragmentDirections.navigateToDetail()
                .setArticleID(articleID);
        Navigation.findNavController(requireView()).navigate(navDirections);
    }
}